<template>
  <div class="xtx-checkbox" @click="handleClickChange">
    <i class="iconfont icon-checked" v-if="flag"></i>
    <i class="iconfont icon-unchecked" v-if="!flag"></i>
    <span><slot></slot></span>
  </div>
</template>

<script>
import { ref, watchEffect } from "vue";

export default {
  name: "XtxCheckbox",
  props: ["modelValue"],
  setup(props, { emit }) {
    // console.log(props);
    // 判断哪个i标签显示
    let flag = ref(false);

    const handleClickChange = () => {
      flag.value = !flag.value;

      emit("update:modelValue", flag.value);
    };

    // 监听props是否发生了改变，如果发生了改变，则两边同步
    watchEffect(() => {
      flag.value = props.modelValue;
    });

    return {
      flag,
      handleClickChange,
    };
  },
};
</script>

<style scoped lang="less">
.xtx-checkbox {
  display: inline-block;
  margin-right: 2px;

  .icon-checked {
    color: @xtxColor;

    ~ span {
      color: @xtxColor;
    }
  }

  i {
    position: relative;
    top: 1px;
  }

  span {
    cursor: pointer;
    margin-left: 2px;
  }
}
</style>
